Imports DotNetNuke
Imports DotNetNuke.Entities.Modules

Public Class ManageComparison
    Inherits PortalModuleBase
    Implements IActionable

    Private Function BuildComparison() As StringBuilder
        Dim output As New StringBuilder
        Dim col_count As Integer = 0
        Dim temp_count As Integer = 0
        Dim list As New ArrayList
        Dim pc As New ProductController
        Dim fc As New FeatureController
        Dim fgc As New FeatureGroupController
        Dim fgi As New FeatureGroupInfo
        Try
            Dim productGroupId As Integer
            Try
                productGroupId = CInt(Me.ddlProductGroups.SelectedValue)
            Catch ex As Exception
                output = New StringBuilder
                output.Append(Services.Localization.Localization.GetString("NoPGInCP.Text", Me.LocalResourceFile))
                Return output
            End Try
            output.Append("<table class=""apc_productcomparison_table "" cellpadding=""0"" cellspacing=""0"">")
            Dim dr As IDataReader = pc.GetProductListByProductGroupId(productGroupId)
            output.Append("<tr class=""apc_product_row"">")
            output.Append("<td></td>")
            col_count += 1
            Do While dr.Read
                output.Append("<td class=""apc_product_cell"">")
                output.Append("<span class=""apc_product"">")
                output.Append(Convert.ToString(dr("Product")))
                output.Append("</span")
                output.Append("</td>")
                col_count += 1
            Loop
            output.Append("</tr>")
            dr.Close()
            dr = Nothing
            If (col_count <> 1) Then
                dr = fgc.GetFeatureGroupsByProductGroupId(productGroupId)
                Do While dr.Read
                    fgi.FeatureGroup = Convert.ToString(dr("FeatureGroup"))
                    fgi.FeatureGroupId = CInt(Convert.ToUInt32(dr("FeatureGroupId")))
                    fgi.SortOrder = CInt(Convert.ToUInt32(dr("SortOrder")))
                    list.Insert((fgi.SortOrder - 1), fgi)
                    fgi = New FeatureGroupInfo
                Loop
                dr.Close()
                If (list.Count = 0) Then
                    output = New StringBuilder
                    output.Append(Services.Localization.Localization.GetString("NoFGs.Text", Services.Localization.Localization.GetResourceFile(Me, "Compare.ascx.resx")))
                    Return output
                End If
                Dim i As Integer = 0
                Do While (i <= (list.Count - 1))
                    output.Append("<tr class=""apc_fg_row"">")
                    output.Append(("<td class=""apc_fg_cell"" colspan=""" & Convert.ToString(col_count) & """>"))
                    output.Append("<span class=""apc_fg"">")
                    output.Append(CType(list.Item(i), FeatureGroupInfo).FeatureGroup)
                    output.Append("</span>")
                    output.Append("</td>")
                    output.Append("</tr>")
                    dr = Nothing
                    dr = pc.GetProductFeatures(CType(list.Item(i), FeatureGroupInfo).FeatureGroupId)
                    temp_count = 1

                    Do While dr.Read
                        Dim alt_params As String
                        Dim pic_path As String
                        Dim js_params As String
                        If (temp_count = 1) Then
                            output.Append("<tr class=""apc_feature_row"">")
                            output.Append("<td class=""apc_feature_cell"">")
                            output.Append("<div class=""apc_feature"">")
                            output.Append(Convert.ToString(dr("Feature")))
                            output.Append("</div>")
                            If (Strings.Len(Convert.ToString(dr("Comment"))) > 0) Then
                                output.Append(String.Concat(New String() {"<div style=""display: inline;""><img id=""ac_", Convert.ToString((dr("FeatureId"))), """ onmouseover=""ac_togglePreview(this, ", Convert.ToString((dr("FeatureId"))), ")"" src=""", Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/about.gif"), """ /></div>"}))
                            End If
                            output.Append("</td>")
                        End If
                        Dim left As String = Convert.ToString(dr("SupportValue"))
                        If left.Equals("1") Then
                            alt_params = Services.Localization.Localization.GetString("NAClick.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx"))
                            pic_path = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/warning.gif")
                            js_params = (Convert.ToString(dr("ProductId")) & "|" & Convert.ToString(dr("FeatureId")) & "|2")
                        ElseIf left.Equals("2") Then
                            pic_path = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/unsupported.gif")
                            alt_params = Services.Localization.Localization.GetString("UnsupportedClick.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx"))
                            js_params = (Convert.ToString(dr("ProductId")) & "|" & Convert.ToString(dr("FeatureId")) & "|3")
                        ElseIf left.Equals("3") Then
                            pic_path = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/partial.gif")
                            alt_params = Services.Localization.Localization.GetString("PartialSupportClick.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx"))
                            js_params = (Convert.ToString(dr("ProductId")) & "|" & Convert.ToString(dr("FeatureId")) & "|4")
                        ElseIf left.Equals("4") Then
                            pic_path = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/supported.gif")
                            alt_params = Services.Localization.Localization.GetString("SupportedClick.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx"))
                            js_params = (Convert.ToString(dr("ProductId")) & "|" & Convert.ToString(dr("FeatureId")) & "|1")
                        ElseIf left.Equals("5") Then
                            pic_path = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/note_add.png")
                            alt_params = Services.Localization.Localization.GetString("NoteAdd.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx"))
                            js_params = (Convert.ToString(dr("ProductId")) & "|" & Convert.ToString(dr("FeatureId")) & "|5")
                        Else
                            pic_path = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/warning.gif")
                            alt_params = Services.Localization.Localization.GetString("NAClick.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx"))
                            js_params = (Convert.ToString(dr("ProductId")) & "|" & Convert.ToString(dr("FeatureId")) & "|2")
                        End If
                        output.Append("<td class=""apc_support_value_cell""><span class=""apc_support_value"">")
                        If left.Equals("5") Then
                            output.Append(String.Concat(Convert.ToString(dr("Text"))))
                        Else
                            output.Append(String.Concat(New String() {"<span id=""acsp_", js_params, """><img id=""acimg_", js_params, """ alt=""", alt_params, """ src=""", pic_path, """ onClick=""javascript:apc_UpdateSupportValue_NoRepaint('acsp_", js_params, "', this.id);""></img></span>"}))
                        End If
                        output.Append("</span>")
                        output.Append("</td>")
                        temp_count += 1
                        If (temp_count = col_count) Then
                            output.Append("</tr>")
                            temp_count = 1
                        End If
                    Loop
                    dr.Close()
                    dr = Nothing
                    i += 1
                Loop
                If CBool(Me.Settings.Item("Pricing")) Then
                    dr = pc.GetProductListByProductGroupId(productGroupId)
                    output.Append("<tr class=""apc_pricing_row"">")
                    output.Append("<td class=""apc_price_head_cell""><span class=""apc_price_head"">" & Services.Localization.Localization.GetString("Pricing.Text", Services.Localization.Localization.GetResourceFile(Me, "Compare.ascx.resx")) & "</span></td>")
                    Do While dr.Read
                        output.Append("<td class=""apc_price_cell"">")
                        output.Append("<span class=""apc_price"">")
                        If CBool(dr("DisplayPrice")) Then
                            output.Append(Services.Localization.Localization.GetString("Currency.Text", Services.Localization.Localization.GetResourceFile(Me, "ControlPanel.ascx.resx")) & Convert.ToDecimal((dr("Price"))).ToString("n"))
                        Else
                            output.Append("&nbsp;")
                        End If
                        output.Append("</span>")
                        output.Append("</td>")

                    Loop
                    output.Append("</tr>")
                    dr.Close()
                    dr.Dispose()
                End If
                With output
                    .Append("</table>")
                    .Append("<br />")
                    .Append("<table class=""apc_legend_table "" cellpadding=""0"" cellspacing=""0"">")
                    .Append("<tr class=""apc_legend_head_row""><td class=""apc_legend_head_cell""><span class=""apc_legend_head"">" & Services.Localization.Localization.GetString("Legend.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & "</span></td>")
                    .Append("<tr class=""apc_legend_row""><td class=""apc_legend_cell""><span class=""apc_legend"">")
                    .Append(("<img alt=""" & Services.Localization.Localization.GetString("FeatureComment.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & """ src=""" & Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/about.gif") & """></img> = " & Services.Localization.Localization.GetString("FCMessage.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & "<br />"))
                    .Append(("<img alt=""" & Services.Localization.Localization.GetString("Unsupported.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & """ src=""" & Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/unsupported.gif") & """></img> = " & Services.Localization.Localization.GetString("Unsupported.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & " <br />"))
                    .Append(("<img alt=""" & Services.Localization.Localization.GetString("PartialSupport.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & """ src=""" & Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/partial.gif") & """></img> = " & Services.Localization.Localization.GetString("PartialSupport.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & "<br />"))
                    .Append(("<img alt=""" & Services.Localization.Localization.GetString("Supported.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & """ src=""" & Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/supported.gif") & """></img> = " & Services.Localization.Localization.GetString("Supported.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & "<br />"))
                    .Append(("<img alt=""" & Services.Localization.Localization.GetString("NA.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & """ src=""" & Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/warning.gif") & """></img> = " & Services.Localization.Localization.GetString("NA.Text", Services.Localization.Localization.GetResourceFile(Me, "Legend.resx")) & "<br />"))
                    .Append("</span></td></tr></table>")
                End With
                Return output
            End If
        Catch ex As Exception
            output = New StringBuilder
            output.Append(Services.Localization.Localization.GetString("Error.Text", Services.Localization.Localization.GetResourceFile(Me, "Compare.ascx.resx")))
            Return output
        End Try
        output = New StringBuilder
        output.Append(Services.Localization.Localization.GetString("NoProducts.Text", Services.Localization.Localization.GetResourceFile(Me, "Compare.ascx.resx")))
        Return output
    End Function

    Private Sub BuildProductGroups()
        Me.ddlProductGroups.DataSource = New ProductGroupController().GetProductGroupsByModuleId(Me.ModuleId)
        Me.ddlProductGroups.DataTextField = "ProductGroup"
        Me.ddlProductGroups.DataValueField = "ProductGroupId"
        Me.ddlProductGroups.DataBind()
        If (Me.ddlProductGroups.Items.Count <= 0) Then
            Me.ddlProductGroups.Items.Insert(0, Services.Localization.Localization.GetString("NoneSaved.Text", Me.LocalResourceFile))
            Me.ddlProductGroups.Enabled = False
        End If
    End Sub

    Private Sub ddlProductGroups_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ddlProductGroups.SelectedIndexChanged
        Me.apc_Comparison.InnerHtml = Me.BuildComparison.ToString
    End Sub

    Public ReadOnly Property ModuleActions() As Entities.Modules.Actions.ModuleActionCollection Implements Entities.Modules.IActionable.ModuleActions
        Get
            Dim actions As New Actions.ModuleActionCollection
            actions.Add(Me.GetNextActionID, Services.Localization.Localization.GetString("ControlPanel.Text", Me.LocalResourceFile), "AddContent.Action", "", "", Me.EditUrl, False, DotNetNuke.Security.SecurityAccessLevel.Edit, True, False)
            Return actions
        End Get
    End Property



    Public helperPath As String
    Public ImagePath As String
    Public PreviewText As String
    Public spinner As String
    Public supported As String
    Public unsupported As String
    Public na As String

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.helperPath = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/ACAjaxHelper.aspx")
        Me.spinner = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/animated_loading.gif")
        Me.ImagePath = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/delete.gif")

        Me.supported = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/supported.gif")
        Me.unsupported = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/unsupported.gif")
        Me.na = Me.Page.ResolveUrl("~/DesktopModules/ActiveCompare/img/warning.gif")


        Me.PreviewText = String.Empty
        If Not Me.IsPostBack Then
            Me.BuildProductGroups()
        End If
        Me.apc_Comparison.InnerHtml = Me.BuildComparison.ToString
        Me.Session.Item("CanEdit") = Me.Session.SessionID

        lblProductGroups.Text = Services.Localization.Localization.GetString("ProductGroups.Text", Me.LocalResourceFile)
        imgClose.Src = ImagePath
        imgClose.Alt = Services.Localization.Localization.GetString("CloseFC.Text", Me.LocalResourceFile)
    End Sub
End Class